package com.jvm;

/**
 * @author: 李德才
 * @description:
 * @create: 2021-06-19 18:58
 **/
public class JVM_15_CPU乱序执行 {


    /**
     * https://blog.csdn.net/qq_36623327/article/details/107622833?spm=1001.2014.3001.5501
     * 如果都是顺序执行，是不是永远都不会出现 x = 0 && y = 0 的情况 ？
     **/
    private  /* volatile */  static int x = 0, y = 0;
    private  /* volatile */   static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        boolean flag = true;
        while (flag) {
            i++;
            x = 0;
            y = 0;
            a = 0;
            b = 0;
            Thread one = new Thread(() -> {
                a = 1;
                x = b;
            });
            Thread other = new Thread(() -> {
                b = 1;
                y = a;
            });
            one.start();
            other.start();
            one.join();
            other.join();
            String result = "第" + i + "次 (" + x + "," + y + "）";
            if (x == 0 && y == 0) {
                System.err.println(result);
                flag = false;
            }
        }
    }
}
